#include "UniformBlock.hpp"
#include <vector>

static std::vector<GLuint> uniformBlockLocations;
static GLuint uniformBlockCounter = 1;

static GLuint generateFreeUniformBlockLocation()
{
	if(uniformBlockLocations.size() > 0)
	{
		GLuint blockLocation = uniformBlockLocations.back();
		uniformBlockLocations.pop_back();
		return blockLocation;
	}
	return uniformBlockCounter++;
}

namespace gl
{

UniformBlock::UniformBlock() :
	mBlockLocation(generateFreeUniformBlockLocation())
{
	
}

bool UniformBlock::setBuffer(const gl::UniformBuffer &buffer)
{
	glBindBufferRange(GL_UNIFORM_BUFFER, mBlockLocation, buffer.mUbo, 0, buffer.mStoredByteSize);
	return true;
}

UniformBlock::~UniformBlock()
{
	// This uniform block location is now free to be used.
	uniformBlockLocations.push_back(mBlockLocation);
}

}